package com.fg.cloud.factory.controller.factory;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.finals.FacImages;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.FactoryParamsEntity;
import com.fg.cloud.manager.service.factory.FactoryImagesService;
import com.fg.cloud.manager.service.factory.FactoryParamsService;

/**
 * Description:厂商参数设置
 * 
 * @author shaoqiping
 * @date 2017年8月3日上午9:16:30
 */
@Controller
@RequestMapping("/factoryParams")
public class FactoryParamsController extends BaseController {

	@Autowired
	private FactoryParamsService paramsService;

	@Autowired
	private FactoryImagesService imagesService;

	/**
	 * Description:厂商参数信息显示
	 * 
	 * @param request
	 * @param response
	 * @author shaoqiping
	 * @date 2017年8月3日上午9:16:53
	 */
	@RequestMapping("/facParams")
	@ResponseBody
	public void facPic(HttpServletRequest request, HttpServletResponse response) {
		try {
			FactoryParamsEntity paramsEntity = paramsService.selectByFacId(getCurrentFactoryUser().getFacId());
			Map<String,Object> map = imagesService.selectByFacId(getCurrentFactoryUser().getFacId(),FacImages.ALL);
			map.put("params", paramsEntity);
			doResult(response, true, StatusCode.FINDSUCCESS, map, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:新增/保存厂商参数信息
	 * 
	 * @param request
	 * @param response
	 * @param params
	 * @author shaoqiping
	 * @date 2017年8月3日上午9:17:34
	 */
	@RequestMapping(value = "/saveOrUpdate", method = RequestMethod.POST)
	@ResponseBody
	public void saveOrUpdate(HttpServletRequest request, HttpServletResponse response, FactoryParamsEntity params,
			String logo, String ads, String tCode) {
		try {
			if (params == null) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.INPUT_NULL);
				return;
			}
			params.setFacId(getCurrentFactoryUser().getFacId());
			FactoryParamsEntity temp = paramsService.selectByFacId(getCurrentFactoryUser().getFacId());
			if (temp == null || LongUtils.isEmpty(temp.getId())) {
				setCreateInfo(params, request);
				setUpdateInfo(params, request);
				boolean flag1 = paramsService.insertSelective(params);
				int flag = imagesService.insertSelectives(params.getFacId(), logo, ads, tCode);
				doResult(response, flag > 0 || flag1 ? true : false,
						flag > 0 || flag1 ? StatusCode.SAVESUCCESS : StatusCode.SAVESUCCESS, null,
						flag > 0 || flag1 ? ResultType.ADD_SUCCESS : ResultType.ADD_FAULT);
			} else {
				setUpdateInfo(params, request);
				params.setId(temp.getId());
				boolean flag1=paramsService.updateByPrimaryKeySelective(params);
				int flag = imagesService.insertSelectives(params.getFacId(), logo, ads, tCode);
				doResult(response, flag > 0 || flag1 ? true : false,
						flag > 0 || flag1 ? StatusCode.SAVESUCCESS : StatusCode.SAVESUCCESS, null,
						flag > 0 || flag1 ? ResultType.SAVE_SUCCESS : ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
}
